<template>
  <div class="setting">

    <el-card class="box-card">
      <template>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">

            <el-button id="aaa" type="primary" size="small" style="margin-bottom: 30px;" value="新增角色" @click="addRedactRole">新增角色</el-button>
            <!-- 表格 -->
            <template>
              <el-table
                align="center"
                border
                :data="roleInfo"
                stripe
                style="width:
                100%"
              >
                <el-table-column
                  align="center"
                  label="序号"
                  width="120"
                  type="index"
                />
                <el-table-column
                  align="center"
                  prop="name"
                  label="角色"
                  width="180"
                />
                <el-table-column
                  align="center"
                  prop="description"
                  label="描述"
                />
                <el-table-column
                  align="center"
                  prop="address"
                  label="操作"
                > <template slot-scope="scope">
                  <el-button type="success" size="small">分配权限</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="addRedactRole(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRedactRole(scope.row.id)"
                  >删除</el-button>
                </template>
                </el-table-column>
              </el-table>
              <!-- 分页 -->
              <div class="block">
                <el-pagination
                  layout="prev, pager, next"
                  :page-size="3"
                  :size="pagesize"
                  :total="sum"
                  @current-change="skip"
                />
              </div>
            </template>

          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <div class="hint">
              <i class="el-icon-info" />
              <span>对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改</span>
            </div>
            <el-form label-width="120px">
              <el-form-item label="公司名称" style="width: 500px">
                <el-input :disabled="true" :value="companyInfo.name" />
              </el-form-item>
              <el-form-item label="公司地址" style="width: 500px">
                <el-input :disabled="true" :value="companyInfo.companyAddress" />
              </el-form-item>
              <el-form-item label="公司邮箱" style="width: 500px">
                <el-input :disabled="true" :value="companyInfo.mailbox" />
              </el-form-item>
              <el-form-item label="备注" style="width: 500px">
                <el-input type="textarea" :disabled="true" :value="companyInfo.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </template>

    </el-card>
    <!-- 新增和编辑弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="50%"
      @close="cancel"
    >
      <el-form ref="ruleForm" :model="roleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="btnok">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyInfo } from '@/api/company'
import { getRoleInfo, addRole, getRoleInfoId, editRoleInfoId, delRoleInfoId } from '@/api/role'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      title: '新增角色',
      // 新增编辑角色
      roleForm: {
        name: '',
        description: ''
      },
      // 效验规则
      rules: {
        name: [
          { required: true, message: '角色名称必填', trigger: 'blur' }
        ]
      },
      activeName: 'first',
      // 公司角色数据
      roleInfo: [],
      // 公司信息
      companyInfo: {},
      // 公司角色页码
      page: 1,
      pagesize: 3,
      // 公司角色总数据数量
      sum: 0,
      dialogVisible: false// 控制新增和隐藏模态框显示和隐藏
    }
  }, computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    // 获取角色数据
    this.init()
    // 获取公司数据
    getCompanyInfo(this.companyId).then(res => {
      this.companyInfo = res
    })
  },

  methods: {
    // 删除角色
    async delRedactRole(id) {
      this.$confirm('是否删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRoleInfoId(id)
        this.init()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 打开模态框判断是编辑还是新增
    async addRedactRole(row) {
      row.id ? this.title = '编辑角色' : this.title = '新增角色'
      this.dialogVisible = true
      // 根据id判断是编辑还是新增
      if (row.id) {
        // 编辑
        const res = await getRoleInfoId(row.id)
        // 给表单赋值实现编辑的数据回填
        this.roleForm = res
      } else {
        // 新增
        this.roleForm = {
          name: '',
          description: ''
        }
      }
    },
    // 取消新增和编辑
    cancel() {
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 新增编辑角色
    async btnok() {
      try {
        await this.$refs.ruleForm.validate()
        if (this.roleForm.id) {
        // 编辑
          this.roleForm.companyId = this.companyId
          await editRoleInfoId(this.roleForm)
        } else {
        // 新增
          await addRole(this.roleForm)
        }
        this.dialogVisible = false
        this.init()
        this.$message.success('操作成功')
      } catch (error) {
        this.$message.error('角色名必填')
      }
    },
    // 获取用户数据
    async init() {
      const res = await getRoleInfo({ page: this.page, pagesize: this.pagesize })
      this.roleInfo = res.rows
      this.sum = res.total
    },

    // 点击换页
    skip(data) {
      // console.log(aa)
      this.page = data
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>
html,.setting{
  height: 100%;
  background-color: #f0f2f5;
  padding: 15px 15px 0;
}

.block{
  height: 20px;
  margin-top: 15px;
  position: relative;
  // margin-left: 350px;

}
.el-pagination{
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%,0);

}
.hint{
  height: 30px;
  background-color: #f4f4f5;
  color: #9093a5;
  font-size: 13px;
  line-height: 30px;
  border-radius: 5px;
  margin-bottom: 50px;
  i{
    margin-left: 15px ;
    margin-right: 10px;
    font-size: 14px;
  }
}
</style>
